﻿// CBandListDlg.cpp: 实现文件
//

#include "pch.h"
#include "CBandList.h"
#include "CBandListDlg.h"
#include "afxdialogex.h"
#include "resource.h"

using ImageBands = CBandList::ImageBands;
// CBandListDlg 对话框

IMPLEMENT_DYNAMIC(CBandListDlg, CDialogEx)

CBandListDlg::CBandListDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_BANDLIST, pParent)
{

}

CBandListDlg::~CBandListDlg()
{
}

void CBandListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE_BANDLIST, m_Tree);
}


BEGIN_MESSAGE_MAP(CBandListDlg, CDialogEx)
	ON_COMMAND(ID_BANDLISTFILEIN, &CBandListDlg::OnBandlistfilein)
	ON_BN_CLICKED(IDC_RADIO_R, &CBandListDlg::OnBnClickedRadioR)
	ON_BN_CLICKED(IDC_RADIO_G, &CBandListDlg::OnBnClickedRadioG)
	ON_BN_CLICKED(IDC_RADIO_B, &CBandListDlg::OnBnClickedRadioB)
	ON_NOTIFY(NM_CLICK, IDC_TREE_BANDLIST, &CBandListDlg::OnNMClickTreeBandlist)
	ON_BN_CLICKED(IDC_BUTTON_BASE, &CBandListDlg::OnBnClickedButtonBase)
	ON_BN_CLICKED(IDC_BUTTON_AJUST, &CBandListDlg::OnBnClickedButtonAjust)
	ON_COMMAND(ID_CONTROLPOINT, &CBandListDlg::OnControlpoint)
	ON_COMMAND(ID_BANDLISTFILEOUT, &CBandListDlg::OnBandlistfileout)
END_MESSAGE_MAP()


// CBandListDlg 消息处理程序


BOOL CBandListDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	m_Menu.LoadMenu(IDR_MENU2);

	SetMenu(&m_Menu);
	// TODO:  在此添加额外的初始化

	CButton* radio = (CButton*)GetDlgItem(IDC_RADIO_R);
	iBandFlag = 0;
	radio->SetCheck(1);

	HICON hIcon[4];

	// 加载图标
	hIcon[0] = AfxGetApp()->LoadIconW(IDI_ICONR);
	hIcon[1] = AfxGetApp()->LoadIconW(IDI_ICONG);
	hIcon[2] = AfxGetApp()->LoadIconW(IDI_ICONB);

	// 创建图标序列CImageList对象
	m_imagelist.Create(16, 16, ILC_COLOR32, 3, 3);

	// 将三个图标添加到图像序列
	for (int i = 0; i < 3; i++) {
		m_imagelist.Add(hIcon[i]);
	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CBandListDlg::OnBandlistfilein()
{
	// TODO: 在此添加命令处理程序代码
	CStringArray strArrFilePaths;

	CFileDialog open_img(TRUE,
		NULL,
		NULL,
		OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST,
		_T("Any Known Formats (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg|All Files (*.*)|*.*|"),//指定要打开的文件类型
		NULL);

	open_img.m_ofn.lpstrTitle = _T("请加载相关图层");
	//最多可以打开100个文件
	open_img.m_ofn.nMaxFile = 100 * MAX_PATH;
	open_img.m_ofn.lpstrFile = new TCHAR[open_img.m_ofn.nMaxFile];
	ZeroMemory(open_img.m_ofn.lpstrFile, sizeof(TCHAR) * open_img.m_ofn.nMaxFile);

	if (open_img.DoModal() == IDOK)
	{
		POSITION pos = open_img.GetStartPosition();//获取第一个文件名的位置

		while (pos != NULL) //GetNextPathName()返回当前pos的文件名，并将下一个文件名的位置保存到pos中
			strArrFilePaths.Add(open_img.GetNextPathName(pos));
	}

	size_t   size = strArrFilePaths.GetSize();   //得长度
	for (size_t i = 0; i < size; i++)
	{   //的数据
		CString FilePath = strArrFilePaths.GetAt(i);
		CBandList::ImageBands TempImageBand = CBandList::ReadImageBands(FilePath);

		CBandList::GetInstance().push_ImageBands(TempImageBand);

		HTREEITEM hroot = m_Tree.InsertItem(TempImageBand.strBandName, int(i + 1), 0, TVI_ROOT);//添加一级结点
		HTREEITEM h1 = m_Tree.InsertItem(_T("R：") + TempImageBand.strBandName, int(i + 1), 0, hroot);//添加二级结点
		HTREEITEM h2 = m_Tree.InsertItem(_T("G：") + TempImageBand.strBandName, int(i + 1), 0, hroot);//添加二级结点
		HTREEITEM h3 = m_Tree.InsertItem(_T("B：") + TempImageBand.strBandName, int(i + 1), 0, hroot);//添加二级结点

		m_Tree.Expand(hroot, TVE_EXPAND); //只展开根结点下面一层.
	}
}


void CBandListDlg::OnBnClickedRadioR()
{
	iBandFlag = 0;
	// TODO: 在此添加控件通知处理程序代码
}


void CBandListDlg::OnBnClickedRadioG()
{
	iBandFlag = 1;
	// TODO: 在此添加控件通知处理程序代码
}


void CBandListDlg::OnBnClickedRadioB()
{
	iBandFlag = 2;
	// TODO: 在此添加控件通知处理程序代码
}


void CBandListDlg::OnNMClickTreeBandlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	CPoint point;
	GetCursorPos(&point);//获得鼠标点击的位置
	m_Tree.ScreenToClient(&point);//转化为客户坐标
	UINT uFlags;
	CString CurrentBandName;
	HTREEITEM CurrentItem;
	CString TempName;
	CurrentItem = m_Tree.HitTest(point, &uFlags);//获得当前点击节点的ITEM
	TempName = m_Tree.GetItemText(CurrentItem);//获得节点值
	if (TempName == _T(""))
	{
		return;
	}

   //获得该节点的父节点
	if (TempName.Left(1) == _T("R") || TempName.Left(1) == _T("G") || TempName.Left(1) == _T("B")) // root节点
	{
		HTREEITEM ParentItem = m_Tree.GetParentItem(CurrentItem);
		CurrentBandName = m_Tree.GetItemText(ParentItem);//获得节点值
	}
	else
	{
		CurrentBandName = m_Tree.GetItemText(CurrentItem);//获得节点值
	}

	if (iBandFlag == 0)
	{
		SetDlgItemText(IDC_EDIT_RBAND, CurrentBandName);
		// 为树形控件设置图像序列
		//m_Tree.SetImageList(&m_imagelist, TVSIL_NORMAL);
		CButton* radio;
		radio = (CButton*)GetDlgItem(IDC_RADIO_R);
		radio->SetCheck(0);
		radio = (CButton*)GetDlgItem(IDC_RADIO_G);
		radio->SetCheck(1);
		iBandFlag = 1;
	}
	else if (iBandFlag == 1)
	{
		SetDlgItemText(IDC_EDIT_GBAND, CurrentBandName);
		// 为树形控件设置图像序列
		//m_Tree.SetImageList(&m_imagelist, TVSIL_NORMAL);
		CButton* radio;
		radio = (CButton*)GetDlgItem(IDC_RADIO_G);
		radio->SetCheck(0);
		radio = (CButton*)GetDlgItem(IDC_RADIO_B);
		radio->SetCheck(1);
		iBandFlag = 2;
	}
	else if (iBandFlag == 2)
	{
		SetDlgItemText(IDC_EDIT_BBAND, CurrentBandName);
		// 为树形控件设置图像序列
		//m_Tree.SetImageList(&m_imagelist, TVSIL_NORMAL);
		CButton* radio;
		radio = (CButton*)GetDlgItem(IDC_RADIO_B);
		radio->SetCheck(0);
		radio = (CButton*)GetDlgItem(IDC_RADIO_R);
		radio->SetCheck(1);
		iBandFlag = 0;
	}
}


void CBandListDlg::OnBnClickedButtonBase()
{
	ImageBands TempImage;

	CString Rname, Bname, Gname;

	GetDlgItemText(IDC_EDIT_RBAND, Rname);
	GetDlgItemText(IDC_EDIT_GBAND, Gname);
	GetDlgItemText(IDC_EDIT_BBAND, Bname);

	if (Rname != _T("") && Gname != _T("") && Bname != _T(""))
		CBandList::GetInstance().ShowViewportBase(TempImage, Rname, Gname, Bname, 0);
	else
		AfxMessageBox(_T("波段选择不完全"));
}


void CBandListDlg::OnBnClickedButtonAjust()
{
	ImageBands TempImage;

	CString Rname, Bname, Gname;

	GetDlgItemText(IDC_EDIT_RBAND, Rname);
	GetDlgItemText(IDC_EDIT_GBAND, Gname);
	GetDlgItemText(IDC_EDIT_BBAND, Bname);

	if (Rname != _T("") && Gname != _T("") && Bname != _T(""))
		CBandList::GetInstance().ShowViewportBase(TempImage, Rname, Gname, Bname, 1);
	else
		AfxMessageBox(_T("波段选择不完全"));
}


void CBandListDlg::OnControlpoint()
{
	CBandList::GetInstance().ConPoint = new ControlPoint(1);

	// TODO: 在此添加命令处理程序代码
}


void CBandListDlg::OnBandlistfileout()
{
	CString Rname, Bname, Gname;

	GetDlgItemText(IDC_EDIT_RBAND, Rname);
	GetDlgItemText(IDC_EDIT_GBAND, Gname);
	GetDlgItemText(IDC_EDIT_BBAND, Bname);

	if (Rname != _T("") && Gname != _T("") && Bname != _T(""))
	{
		CString filename;
		LPCTSTR szFilter = _T("Any Known Formats (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg|All Files (*.*)|*.*|");		//提示保存文件的类型
		CFileDialog output_img(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

		if (output_img.DoModal() == IDOK)
		{
			filename = output_img.GetPathName();		//保存文件的路径到filename变量中
		}
		CBandList::GetInstance().OutputImage(filename, Rname, Gname, Bname);

		AfxMessageBox(_T("导出成功"));
	}
	else
		AfxMessageBox(_T("波段选择不完全"));

	
}
